﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyTinnyApi.model
{
    [Table("t_request")]
    public class Requests : INotifyPropertyChanged
    {
        private int id;
        private int folderId;
        private string bread;
        private string requestName;
        private string requestUrl;
        private string requestMethod;
        private int showOrder;
        //private string requestParam;
        //private string requestHeader;
        //private string requestBody;
        private bool initedFullInfo;
        private int? currentShow;
        private string responseText;
        private string description;

        [NotMapped]
        public bool InitedFullInfo
        {
            get
            {
                return initedFullInfo;
            }
            set
            {
                initedFullInfo = value;
                OnPropertyChanged(nameof(InitedFullInfo));
            }
        }

        [NotMapped]
        public int? CurrentShow
        {
            set
            {
                currentShow = value;
                OnPropertyChanged(nameof(ShowParamPanel));
                OnPropertyChanged(nameof(ShowHeaderPanel));
                OnPropertyChanged(nameof(ShowBodyPanel));
            }
        }

        [NotMapped]
        public bool ShowParamPanel
        {
            get
            {
                return currentShow == 0;
            }
        }

        [NotMapped]
        public bool ShowHeaderPanel
        {
            get
            {
                return currentShow == 1;
            }
        }

        [NotMapped]
        public bool ShowBodyPanel
        {
            get
            {
                return currentShow == 2;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
                OnPropertyChanged(nameof(Description));
            }
        }

        [NotMapped]
        public string ResponseText
        {
            get
            {
                return responseText;
            }
            set
            {
                responseText = value;
                OnPropertyChanged(nameof(ResponseText));
            }
        }

        /// <summary>
        /// url完整信息
        /// </summary>
        [NotMapped]
        public string FullRequestUrl
        {
            get
            {
                var url = RequestUrl;
                if(RequestParamList?.Count > 0)
                {
                    var param = new List<string>();
                    foreach(var rp in RequestParamList)
                    {
                        if(rp.InUse.GetValueOrDefault(false))
                        {
                            param.Add($"{rp.ParamName}={rp.ParamValue}");
                        }
                    }
                    if(param.Count > 0)
                    {
                        url += "?" + string.Join("&", param);
                    }
                }
                return url;
            }
        }

        private List<RequestParam> _requestParamList;
        [NotMapped]
        public List<RequestParam> RequestParamList
        {
            get
            {
                return _requestParamList;
            }
            set
            {
                _requestParamList = value;
                _requestParamList?.ForEach(p =>
                {
                    p.PropertyChanged += P_PropertyChanged;
                });
                //foreach (var p in _requestParamList)
                //{
                //    p.PropertyChanged += P_PropertyChanged;
                //}
                OnPropertyChanged(nameof(RequestParamList));
                OnPropertyChanged(nameof(FullRequestUrl));
            }
        }

        private void P_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(nameof(RequestParamList));
            OnPropertyChanged(nameof(FullRequestUrl));
        }

        private List<RequestHeader> _requestHeaderList;
        [NotMapped]
        public List<RequestHeader> RequestHeaderList
        {
            get
            {
                return _requestHeaderList;
            }
            set
            {
                _requestHeaderList = value;
                if(_requestParamList?.Count > 0)
                {
                    foreach (var p in _requestParamList)
                    {
                        p.PropertyChanged += P_PropertyChanged;
                    }
                }
                OnPropertyChanged(nameof(RequestHeaderList));
            }
        }

        private RequestBody _requestBody;
        [NotMapped]
        public RequestBody RequestBodyModel
        {
            get
            {
                return _requestBody;
            }
            set
            {
                _requestBody = value;
                //foreach (var p in _requestParamList)
                //{
                //    p.PropertyChanged += P_PropertyChanged;
                //}
                OnPropertyChanged(nameof(RequestBodyModel));
            }
        }

        public int Id
        {
            get { return id; }
            set
            {
                if (id != value)
                {
                    id = value;
                    OnPropertyChanged(nameof(Id));
                }
            }
        }

        public int FolderId
        {
            get { return folderId; }
            set
            {
                if (folderId != value)
                {
                    folderId = value;
                    OnPropertyChanged(nameof(FolderId));
                }
            }
        }

        //[NotMapped]
        //public string Bread
        //{
        //    get { return bread; }
        //    set
        //    {
        //        if (bread != value)
        //        {
        //            bread = value;
        //            OnPropertyChanged(nameof(Bread));
        //        }
        //    }
        //}

        public string RequestName
        {
            get { return requestName; }
            set
            {
                if (requestName != value)
                {
                    requestName = value;
                    OnPropertyChanged(nameof(RequestName));
                }
            }
        }

        public string RequestUrl
        {
            get { return requestUrl; }
            set
            {
                if (requestUrl != value)
                {
                    requestUrl = value;
                    OnPropertyChanged(nameof(RequestUrl));
                }
            }
        }

        public string RequestMethod
        {
            get { return requestMethod?.ToUpper() ?? "GET"; }
            set
            {
                if (requestMethod != value)
                {
                    requestMethod = value;
                    OnPropertyChanged(nameof(RequestMethod));
                }
            }
        }

        public int ShowOrder
        {
            get { return showOrder; }
            set
            {
                if (showOrder != value)
                {
                    showOrder = value;
                    OnPropertyChanged(nameof(ShowOrder));
                }
            }
        }

        //public string RequestParam
        //{
        //    get { return requestParam; }
        //    set
        //    {
        //        if (requestParam != value)
        //        {
        //            requestParam = value;
        //            OnPropertyChanged(nameof(RequestParam));
        //        }
        //    }
        //}

        //public string RequestHeader
        //{
        //    get { return requestHeader; }
        //    set
        //    {
        //        if (requestHeader != value)
        //        {
        //            requestHeader = value;
        //            OnPropertyChanged(nameof(RequestHeader));
        //        }
        //    }
        //}

        //public string RequestBody
        //{
        //    get { return requestBody; }
        //    set
        //    {
        //        if (requestBody != value)
        //        {
        //            requestBody = value;
        //            OnPropertyChanged(nameof(RequestBody));
        //        }
        //    }
        //}

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [Table("t_request_param")]
    public class RequestParam : INotifyPropertyChanged
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        private int _requestId;
        public int RequestId
        {
            get { return _requestId; }
            set
            {
                _requestId = value;
                OnPropertyChanged(nameof(RequestId));
            }
        }

        private int? _paramVersion;
        public int? ParamVersion
        {
            get { return _paramVersion; }
            set
            {
                _paramVersion = value;
                OnPropertyChanged(nameof(ParamVersion));
            }
        }

        private int? _showOrder;
        public int? ShowOrder
        {
            get { return _showOrder; }
            set
            {
                _showOrder = value;
                OnPropertyChanged(nameof(ShowOrder));
            }
        }

        private bool? _inUse;
        public bool? InUse
        {
            get { return _inUse; }
            set
            {
                _inUse = value;
                OnPropertyChanged(nameof(InUse));
            }
        }

        private string _paramName;
        public string ParamName
        {
            get { return _paramName; }
            set
            {
                _paramName = value;
                OnPropertyChanged(nameof(ParamName));
            }
        }

        private string _paramValue;
        public string ParamValue
        {
            get { return _paramValue; }
            set
            {
                _paramValue = value;
                OnPropertyChanged(nameof(ParamValue));
            }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged(nameof(Description));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [Table("t_request_header")]
    public class RequestHeader : INotifyPropertyChanged
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        private int _requestId;
        public int RequestId
        {
            get { return _requestId; }
            set
            {
                _requestId = value;
                OnPropertyChanged(nameof(RequestId));
            }
        }

        private int? _headerVersion;
        public int? HeaderVersion
        {
            get { return _headerVersion; }
            set
            {
                _headerVersion = value;
                OnPropertyChanged(nameof(HeaderVersion));
            }
        }

        private int? _showOrder;
        public int? ShowOrder
        {
            get { return _showOrder; }
            set
            {
                _showOrder = value;
                OnPropertyChanged(nameof(ShowOrder));
            }
        }

        private bool? _inUse;
        public bool? InUse
        {
            get { return _inUse; }
            set
            {
                _inUse = value;
                OnPropertyChanged(nameof(InUse));
            }
        }

        private string _headerName;
        public string HeaderName
        {
            get { return _headerName; }
            set
            {
                _headerName = value;
                OnPropertyChanged(nameof(HeaderName));
            }
        }

        private string _headerValue;
        public string HeaderValue
        {
            get { return _headerValue; }
            set
            {
                _headerValue = value;
                OnPropertyChanged(nameof(HeaderValue));
            }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged(nameof(Description));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [Table("t_request_body")]
    public class RequestBody : INotifyPropertyChanged
    {
        private int _id;
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        private int _requestId;
        public int RequestId
        {
            get
            {
                return _requestId;
            }
            set
            {
                _requestId = value;
                OnPropertyChanged(nameof(RequestId));
            }
        }

        private string _bodyType;
        public string BodyType
        {
            get
            {
                return _bodyType;
            }
            set
            {
                _bodyType = value;
                OnPropertyChanged(nameof(BodyType));
            }
        }

        private int? _showOrder;
        public int? ShowOrder
        {
            get
            {
                return _showOrder;
            }
            set
            {
                _showOrder = value;
                OnPropertyChanged(nameof(ShowOrder));
            }
        }

        private int? _inUse;
        public int? InUse
        {
            get
            {
                return _inUse;
            }
            set
            {
                _inUse = value;
                OnPropertyChanged(nameof(InUse));
            }
        }

        private string _bodyContent;
        public string BodyContent
        {
            get
            {
                return _bodyContent;
            }
            set
            {
                _bodyContent = value;
                OnPropertyChanged(nameof(BodyContent));
            }
        }

        private List<RequestBodyParam> _bodyParams;
        [NotMapped]
        public List<RequestBodyParam> BodyParams
        {
            get
            {
                return _bodyParams;
            }
            set
            {
                _bodyParams = value;
                _bodyParams.ForEach(bp =>
                {
                    bp.PropertyChanged += Bp_PropertyChanged;
                });
                OnPropertyChanged(nameof(BodyParams));
            }
        }

        private int _bodyVersion;
        public int BodyVersion
        {
            get
            {
                return _bodyVersion;
            }
            set
            {
                _bodyVersion = value;
                OnPropertyChanged(nameof(BodyVersion));
            }
        }

        private void Bp_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(nameof(BodyParams));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [Table("t_request_body_param")]
    public class RequestBodyParam : INotifyPropertyChanged
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        private int _bodyId;
        public int BodyId
        {
            get { return _bodyId; }
            set
            {
                _bodyId = value;
                OnPropertyChanged(nameof(BodyId));
            }
        }

        private int? _paramVersion;
        public int? ParamVersion
        {
            get { return _paramVersion; }
            set
            {
                _paramVersion = value;
                OnPropertyChanged(nameof(ParamVersion));
            }
        }

        private int? _showOrder;
        public int? ShowOrder
        {
            get { return _showOrder; }
            set
            {
                _showOrder = value;
                OnPropertyChanged(nameof(ShowOrder));
            }
        }

        private bool? _inUse;
        public bool? InUse
        {
            get { return _inUse; }
            set
            {
                _inUse = value;
                OnPropertyChanged(nameof(InUse));
            }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged(nameof(Description));
            }
        }

        private string _paramName;
        public string ParamName
        {
            get { return _paramName; }
            set
            {
                _paramName = value;
                OnPropertyChanged(nameof(ParamName));
            }
        }

        private string _paramValue;
        public string ParamValue
        {
            get { return _paramValue; }
            set
            {
                _paramValue = value;
                OnPropertyChanged(nameof(ParamValue));
            }
        }

        private string _rawContent;
        public string RawContent
        {
            get { return _rawContent; }
            set
            {
                _rawContent = value;
                OnPropertyChanged(nameof(RawContent));
            }
        }

        private string _bodyType;
        public string BodyType
        {
            get { return _bodyType; }
            set
            {
                _bodyType = value;
                OnPropertyChanged(nameof(BodyType));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }



    public class RequestTab : INotifyPropertyChanged
    {
        private Requests _req;
        public Requests Request
        {
            get
            {
                return _req;
            }
            set
            {
                _req = value;
                OnPropertyChanged(nameof(Request));
            }
        }

        private bool _actived;
        public bool Actived
        {
            get
            {
                return _actived;
            }
            set
            {
                _actived = value;
                OnPropertyChanged(nameof(Actived));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

